package com.wms.controller;

import com.wms.dto.LogisticsCompanyDto;
import com.wms.pojo.LogisticsCompany;
import com.wms.pojo.PageResult;
import com.wms.pojo.Result;
import com.wms.service.LogisticsCompanyService;
import com.wms.vo.LogisticsCompanyNameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/logisticscompany")
public class LogisticsCompanyController {
    @Autowired
    private LogisticsCompanyService logisticsCompanyService;

    @GetMapping
    public Result queryPage(Integer page,Integer pageSize ,String companyName,String linkUser){
        PageResult pageResult = logisticsCompanyService.queryPage(page,pageSize,companyName,linkUser);
        return Result.success(pageResult);
    }

    //新增
    @PostMapping
    public Result saveLogisticsCompany(@RequestBody LogisticsCompanyDto dto){
        LogisticsCompany logisticsCompany = LogisticsCompany.builder().companyName(dto.getCompanyName())
                .linkUser(dto.getLinkUser())
                .linkePhone(dto.getLinkePhone())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .minPrice(dto.getMinPrice())
                .build();
        logisticsCompanyService.save(logisticsCompany);
        return Result.success();
    }

    //回显
    @GetMapping("/{id}")
    public Result getLogisticsCompanyById(@PathVariable Integer id){
        LogisticsCompany logisticsCompany = logisticsCompanyService.getById(id);
        LogisticsCompanyDto logisticsCompanyDto = LogisticsCompanyDto.builder().id(logisticsCompany.getId())
                .companyName(logisticsCompany.getCompanyName())
                .minPrice(logisticsCompany.getMinPrice())
                .linkePhone(logisticsCompany.getLinkePhone())
                .linkUser(logisticsCompany.getLinkUser()).build();
        return Result.success(logisticsCompanyDto);
    }

    //修改
    @PutMapping
    public Result updateById(@RequestBody LogisticsCompanyDto dto){
        LogisticsCompany logisticsCompany = LogisticsCompany.builder().id(dto.getId())
                .companyName(dto.getCompanyName())
                .linkUser(dto.getLinkUser())
                .linkePhone(dto.getLinkePhone())
                .updateTime(LocalDateTime.now())
                .minPrice(dto.getMinPrice())
                .build();
        logisticsCompanyService.updateById(logisticsCompany);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        logisticsCompanyService.removeById(id);
        return Result.success();
    }

    //查询所有的公司名称
    @GetMapping("/all")
    public Result getCompanyName(){
        List<LogisticsCompany> logisticsCompanies = logisticsCompanyService.getBaseMapper().selectList(null);
        List<LogisticsCompanyNameVo> list = new ArrayList<>();
        for (LogisticsCompany logisticsCompany : logisticsCompanies) {
            LogisticsCompanyNameVo nameVo = new LogisticsCompanyNameVo();
            nameVo.setCompanyName(logisticsCompany.getCompanyName());
            nameVo.setId(logisticsCompany.getId());
            list.add(nameVo);
        }
        return Result.success(list);
    }
}
